package cn.cms.chuangjia.vo;

import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//本类封装了分页的数据
public class Page<T> implements Serializable {
    public static final int PAGESIZE=10;
    private int code=0;
    //本页数据
    private List<T> data = new ArrayList<>(0);
    private int pageNo = 1;///
    private long count;//总数
    private long pageTotal;//总页数
    private int pageSize = PAGESIZE;//

    public Page(){

    }
    public Page(List<T> data, long total, int pageSize) {
        this.data = data;
//        this.count = count;
        this.pageSize = pageSize;
        this.setCount(total);
    }
    public Page(List<T> data, int pageNo, long total, int pageSize) {
        //System.out.println(">>>>>>>>>"+pageSize+">>>>>>>>>>>>");
        this.data = data;
        this.pageNo = pageNo;
//        this.count = count;
        this.pageSize = pageSize;
        this.setCount(total);

    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
        if(this.pageSize<=0)
            return;
        this.pageTotal = this.count / this.pageSize + (this.count % this.pageSize == 0 ? 0 : 1);
    }

    public long getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(long pageTotal) {
        this.pageTotal = pageTotal;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public  static <T> Page convert(PageInfo<T> p){
        return new Page<T>(p.getList(),p.getPageNum(),p.getTotal(),p.getPageSize());
    }
}
